<?php
////////////////// ENGLISH COURSE POST //////////////////
add_action('init', 'create_english_course_post_type');
function create_english_course_post_type()
{
    register_post_type('english-course', array(
        'labels' => array(
            'name' => __('English Courses'),
            'singular_name' => __('English Course'),
            'add_new' => __('Add New'),
            'add_new_item' => __('Add New English Course'),
            'edit' => __('Edit'),
            'edit_item' => __('Edit Course'),
            'new_item' => __('New Course'),
            'view' => __('View Course'),
            'view_item' => __('View this course'),
            'search_items' => __('Search Courses'),
            'not_found' => __('No Courses found'),
            'not_found_in_trash' => __('No Courses found in Trash')),
        'public' => true,
        'show_ui' => true,
        'publicy_queryable' => true,
        'exclude_from_search' => false,
        'menu_position' => 20,
        'menu_icon' => assets('images/course.png'),
        'hierarchical' => false,
        'query_var' => true,
        'supports' => array(
            'title',
            'editor',
            'comments',
            'author',
            'thumbnail'),
        'rewrite' => array('slug' => 'english-course', 'with_front' => false),
        //'taxonomies' =>  array('post_tag', 'category'),
        'can_export' => true,
        //'register_meta_box_cb'  =>  'call_to_function_do_something',
        'description' => __('Course description here.')));
}

add_action('add_meta_boxes', 'add_ec_meta_box');
function add_ec_meta_box()
{
    add_meta_box('ec_title_vi',        'Course title (VI)',               'render_ec_title_vi',        'english-course', 'normal', 'high');
    add_meta_box('ec_desc_header_en',  'Courses description header (EN)', 'render_ec_desc_header_en',  'english-course', 'normal', 'high');
    add_meta_box('ec_desc_header_vi',  'Courses description header (VI)', 'render_ec_desc_header_vi',  'english-course', 'normal', 'high');
    add_meta_box('ec_desc_content_en', 'Courses description (EN)',        'render_ec_desc_content_en', 'english-course', 'normal', 'high');
    add_meta_box('ec_desc_content_vi', 'Courses description (VI)',        'render_ec_desc_content_vi', 'english-course', 'normal', 'high');
}

function render_ec_title_vi(){
    $values = get_post_custom($post->ID);
    $text = isset($values['ec_title_vi']) ? esc_attr($values['ec_title_vi'][0]) : "";
    wp_nonce_field('my_meta_box_ec_nonce', 'ec_nonce');
    echo '<input type="text" name="ec_title_vi" value="' . $text . '" size="100"/>';
}

function render_ec_desc_header_en(){
    $values = get_post_custom($post->ID);
    $text = isset($values['ec_desc_header_en']) ? esc_attr($values['ec_desc_header_en'][0]) : "";
    echo '<input type="text" name="ec_desc_header_en" value="' . $text . '" size="100"/>';
}

function render_ec_desc_header_vi(){
    $values = get_post_custom($post->ID);
    $text = isset($values['ec_desc_header_vi']) ? esc_attr($values['ec_desc_header_vi'][0]) : "";
    echo '<input type="text" name="ec_desc_header_vi" value="' . $text . '" size="100"/>';
}

function render_ec_desc_content_en(){
    $values = get_post_custom($post->ID);
    $text = isset($values['ec_desc_content_en']) ? esc_attr($values['ec_desc_content_en'][0]) : "";
    echo '<textarea name="ec_desc_content_en" cols="100" rows="8">' . $text . '</textarea>';
}

function render_ec_desc_content_vi(){
    $values = get_post_custom($post->ID);
    $text = isset($values['ec_desc_content_vi']) ? esc_attr($values['ec_desc_content_vi'][0]) : "";
    echo '<textarea name="ec_desc_content_vi" cols="100" rows="8">' . $text . '</textarea>';
}

//process action
add_action('save_post', 'save_ec');
function save_ec($post_id)
{
    // Fail if we're doing an auto save
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;

    // if our nonce isn't there, or we can't verify it, bail
    if (!isset($_POST['ec_nonce']) || !wp_verify_nonce($_POST['ec_nonce'], 'my_meta_box_ec_nonce')) return;

    // if our current user can't edit this post, bail
    if (!current_user_can('edit_post'))return;

    // now we can actually save the data
    $allowed = array('a' => array('href' => array()));

    $aPost = array('ec_title_vi', 'ec_desc_header_en', 'ec_desc_header_vi', 'ec_desc_content_en', 'ec_desc_content_vi');
    foreach ($aPost as $p) {
        if(!isset($_POST[$p])){
            return;
        }
    }
    foreach ($aPost as $p) {
        update_post_meta($post_id, $p, wp_kses($_POST[$p], $allowed));
    }
}
/////////////////////////////// END ENGLISH COURSE ////////////////////////////////////////////
?>